package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.CustodianEntity;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class CustodianProcessor implements PageProcessor {

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		String url = page.getRequest().getUrl();
		Element table = doc.select("div.innerC.mlAuto > div.innerCenter > div.iRight > div.ldContent > div > table").first();
		Elements trs = table.select("tr:gt(0)");
		for (Element tr : trs) {
			CustodianEntity cust = new CustodianEntity();
			Elements tds = tr.select("td");
			cust.setCustodianName(tds.get(1).text());
			cust.setRegisterAddress(tds.get(2).text());
			cust.setQuaDate(tds.get(3).text());
			cust.setOfficialWebsite(tds.get(4).text());
			cust.setCsTel(tds.get(5).text());
			cust.setUrl(url);
			cust.setId(UuIdPublic.timeStrongID(5));
			cust.setCreateTime(new Timestamp(System.currentTimeMillis()));
			StringUtils.removeSpace(cust);
			objectContainer.add(cust);
		}

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub

	}

}
